package com.doxiaopu.students.product.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.product.service.ISingleService;
import com.doxiaopu.students.product.vo.AdminSingleParameterVO;
import com.doxiaopu.students.product.vo.SingleVO;
import com.doxiaopu.students.product.vo.StatusVO;
import com.doxiaopu.students.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;
@Api(tags = "团单信息")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class SingleController extends BaseController {

    @Autowired
    private ISingleService iSingleService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //创建团单
    @ApiOperation(value = "创建团单")
    @PostMapping("/CreateSingle")
    @ResponseBody
    public ResponseResult<User> createSingle(@RequestBody SingleVO singleVO, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        String uuid  =  UUID.randomUUID().toString();
        singleVO.setFlagid(uuid);
        iSingleService.createSinle(singleVO,id);
        return new ResponseResult<User>(SUCCESS);
    }

    //通过团单flagid获得团单详情信息
    @ApiOperation(value = "查询团单详情信息")
    @PostMapping("/GetSingle")
    @ResponseBody
    public ResponseResult<SingleVO> getSingleVO(String flagid ){
        SingleVO singleVO = iSingleService.getSingle(flagid);
        return new ResponseResult<SingleVO>(SUCCESS,singleVO);
    }

    //    app商品列表获取接口
    @ApiOperation(value = "查询团单列表信息")
    @PostMapping("/GetSingleList")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getSingleVOList(){
        AdminSingleParameterVO adminSingleParameterVO = new AdminSingleParameterVO();
        adminSingleParameterVO.setSaleStatus(1);
        Integer id = null;
        Map<String, Object> singleVO = iSingleService.getSingleList(adminSingleParameterVO,id);
        return new ResponseResult<Map<String, Object>>(SUCCESS,singleVO);
    }

    //管理后台商品列表获取接口
    @ApiOperation(value = "查询管理后台商品列表信息")
    @PostMapping("/GetAdminSingleList")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getAdminSingleVOList(HttpServletRequest request, AdminSingleParameterVO adminSingleParameterVO){
        Integer id =getUidFromRequest(request);
        Map<String, Object> singleVO = iSingleService.getSingleList(adminSingleParameterVO,id);
        return new ResponseResult<Map<String, Object>>(SUCCESS,singleVO);
    }

    //管理后台商品列表获取接口
    @ApiOperation(value = "查询用户端商品列表信息")
    @PostMapping("/getAppSingleVOList")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getAppSingleVOList(HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            Map<String, Object> singleVO = iSingleService.getAppSingleList();
            return new ResponseResult<Map<String, Object>>(SUCCESS,singleVO);
        }else {
            return new ResponseResult<Map<String, Object>>(408,"登录过期请重新登录");
        }
    }

    // 更改团单信息
    @ApiOperation(value = "修改团单信息")
    @PostMapping("/UpdatetSingle")
    @ResponseBody
    public ResponseResult<Void> updateSingle(@RequestBody SingleVO singleVO, HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        iSingleService.updateSingle(singleVO,id);
        return new ResponseResult<Void>(SUCCESS);
    }

    //更改团单的售卖状态
    @ApiOperation(value = "修改团单的售卖状态")
    @PostMapping("/updateSaleStatus")
    @ResponseBody
    public ResponseResult<Void> updateSaleStatus(HttpServletRequest request,StatusVO statusVO){
        Integer id =getUidFromRequest(request);
        Integer row = iSingleService.updateSale(statusVO,id);
        System.err.print(row);
        return new ResponseResult<Void>(SUCCESS);
    }

    //更改团单的返利状态和返利金额
    @ApiOperation(value = "修改团单的返利状态和返利金额")
    @PostMapping("/updateRebateStatus")
    @ResponseBody
    public ResponseResult<Void> updateRebateStatus(HttpServletRequest request,StatusVO statusVO){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            iSingleService.updateRebate(statusVO,id);
            return new ResponseResult<Void>(SUCCESS);
        }else {
            return new ResponseResult<Void>(408,"登录过期请重新登录");
        }

    }
    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        if(stringRedisTemplate.opsForValue().get(token) != null) {
            return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
        }else {
            return null;
        }
    }
}
